package functions.ems_claim;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.LoggerFactory;
import com.beans.PscAuditUserBean;
import com.crc.mam.BaseCase;
import coms.Constants;
import frame.HttpHelper;

/**
 * 
 * @author CHENHAILIAN6
 *
 */
public class MyClaim extends BaseCase{
  
  private static org.slf4j.Logger logger = LoggerFactory.getLogger(MyClaim.class);
  
  /**
   * 在我的已提交里面，通过claimNo，查询下一个审批人
   * @param claimNo
   */
  public PscAuditUserBean getMySubmitClaim(String claimNo) {
    PscAuditUserBean auditUserBean = null;
    logger.info("getMySubmitClaim  --- start ---- ");
    String url = Constants.GET_MY_SUBMIT_CLAIM_URL;
    String params =  "{\"page\":{\"currentPage\":1,\"pageSize\":10,\"recordCount\":33},"
          + "\"params\":{\"catalogyCodeList\":[],\"extend1\":\"\",\"extend2\":\"\",\"endapplyDate\":\"\",\"startapplyDate\":\"\",\"ouCode\":\"\",\"submissionOuName\":\"\",\"vendorCode\":\"\",\"vendorName\":\"\","
          + "\"claimNo\":\""+claimNo+"\",\"remark\":\"\",\"submitter\":\"\",\"processState\":\"\"},"
          + "\"sortDTO\":{\"fieldName\":\"\",\"orderBy\":\"\"}}";

    logger.info("getMySubmitClaim params --- " + params);
    try {
      result = HttpHelper.post(params, url);
      status = HttpHelper.isOk;
      logger.info("getMySubmitClaim --- " + result);
      if(status) {
        try {
          JSONObject object = new JSONObject(result);
          int total = object.getInt("total");
          if(total >0) {
            JSONArray array = object.getJSONArray("list");
            JSONObject datas = array.getJSONObject(0);
            if(datas.get("ldapName") != null) {
              String ldapCode = datas.getString("ldapCode");
              String ldapName = String.valueOf(datas.get("ldapName"));
              auditUserBean = new PscAuditUserBean(ldapCode,ldapName);
              remark = auditUserBean.toString();
              logger.info(remark);
            }else {
              remark = "审批人为空";
              logger.error(remark);
            }
          }else{
            status = false;
            remark = "在我的已提交找不到报账单： " + claimNo;
          }
        }catch(JSONException e) {
          e.printStackTrace();
          remark = "JSON解析异常";
        }
      }else {
        try {
          JSONObject object = new JSONObject(result);
          remark = object.getString("message");
        }catch(JSONException e) {
          e.printStackTrace();
          remark = "JSON解析异常";
        }
      }
    }catch(Exception e) {
      status = false;
      remark = "系统异常";
    }
    logger.info("getMySubmitClaim  --- end ---- ");
    return auditUserBean;
  }

}
